#include <uci.h>
#include "data_type.h"
#include "idc_ubus.h"
#include "ep_gpio.h"

#define UCI_CONFIG_FILE "/etc/config/idc_mesh"
#define NUM_THREADS             6
MeshCfg meshCfg;
/*********************************************
* 载入配置文件,并遍历Section.
*/
void idc_load_config()
{
    struct uci_package * pkg = NULL;
    struct uci_element *e;
    struct uci_context * ctx = NULL;
    char *tmp;
    const char *value;

    ctx = uci_alloc_context(); // 申请一个UCI上下文.
    if (UCI_OK != uci_load(ctx, UCI_CONFIG_FILE, &pkg))
        goto cleanup; //如果打开UCI文件失败,则跳到末尾 清理 UCI 上下文.


    /*遍历UCI的每一个节*/
    uci_foreach_element(&pkg->sections, e)
    {
        struct uci_section *s = uci_to_section(e);
        if(!strcmp("conf",s->type)) //this section is a meter
        {
            /**网桥数据读取配置**/
            if (NULL != (value = uci_lookup_option_string(ctx, s, "ipaddr")))
            {
                meshCfg.ipaddr = strdup(value);
            }
            if (NULL != (value = uci_lookup_option_string(ctx, s, "username")))
            {
                meshCfg.username = strdup(value);
            }
            if (NULL != (value = uci_lookup_option_string(ctx, s, "password")))
            {
                meshCfg.password = strdup(value);
            }
        }
    }
    uci_unload(ctx, pkg); // 释放 pkg
    cleanup:
    uci_free_context(ctx);
    ctx = NULL;
}

void init(){
    idc_load_config();
    // ep_gpio_export(448);
    // ep_gpio_direction(448,2);
}
int main(int argc, char* argv[])
{
    init();
    
    pthread_t th_ubus;
    pthread_create(&th_ubus, NULL, (void*)ubus_worker, NULL);
    while (true)
    {
        sleep(1);
    }
}
